package com.baseDemo.web.dao.navigation.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.baseDemo.web.dao.navigation.NavigationEntityDao;
import com.baseDemo.web.entity.NavigationEntity;
import com.share.hibernate.baseDaoSupport.impl.BaseDaoHibernateSupport;
@Repository
public class NavigationEntityDaoHibernate extends BaseDaoHibernateSupport<NavigationEntity> implements NavigationEntityDao{
	private static Map<String, Object> paramMap = new HashMap<String, Object>();
	@Override
	public List<NavigationEntity> findByParentId(String parentid) {
		final String HQL="from NavigationEntity where parentId=:parentid";  
		paramMap.clear();
		paramMap.put("parentid", parentid);
		return super.find(HQL,paramMap,true);
	}

	@Override
	public String updateNav(NavigationEntity navigationEntity) {
		final String HQL="update NavigationEntity ne set ne.appName=:appName,ne.clazz=:clazz,ne.controller=:controller,ne.icon=:icon,ne.leaf=:leaf,ne.parentId=:parentId,ne.text=:text where ne.id = :id ";  
		paramMap.clear();
		paramMap.put("appName", navigationEntity.getAppName());
		paramMap.put("clazz", navigationEntity.getClazz());
		paramMap.put("controller", navigationEntity.getController());
		paramMap.put("icon", navigationEntity.getIcon());
		paramMap.put("leaf", navigationEntity.isLeaf());
		paramMap.put("parentId", navigationEntity.getParentId());
		paramMap.put("text", navigationEntity.getText());
		paramMap.put("id", navigationEntity.getId());
		return this.execute(HQL, paramMap);
	}

	@Override
	public List<NavigationEntity> getNavByLeafFlase() {
		final String HQL = "from NavigationEntity ne where ne.leaf = true ";
		return super.find(HQL);
	}

	@Override
	public List<NavigationEntity> getChildrenByAuth(String parentId,
			List<String> menuIdList) {
		//final String HQL = "from NavigationEntity ne where  ( (ne.leaf=false and (select count(ch.id) from NavigationEntity ch where ch.parentId = ne.id and (select count(tme.id) from ToolbarMenuEntity tme where tme.navigationid = ch.id AND tme.id IN ( :idlist ))>0 )>0 ) or  ( ne.leaf=true AND  (select count(te.id) from ToolbarMenuEntity te where te.navigationid = ne.id AND te.id IN ( :idlist ))>0) ) and ne.parentId=:parentId";
		final String HQL = "from NavigationEntity ne where  ( (ne.leaf=false and (select count(ch.id) from NavigationEntity ch where ch.parentId = ne.id and (select count(tme.id) from AuthorityEntity tme where tme.navigationid = ch.id AND tme.id IN ( :idlist ))>0 )>0 ) or  ( ne.leaf=true AND  (select count(te.id) from AuthorityEntity te where te.navigationid = ne.id AND te.id IN ( :idlist ))>0) ) and ne.parentId=:parentId";
		paramMap.clear();
		paramMap.put("parentId", parentId);
		paramMap.put("idlist", menuIdList);
		return super.find(HQL,paramMap,true);
	}

}
